/**
 *
 */
package com.angel.architecture.io.rowProcessors;

import com.angel.architecture.daos.ConfigurationParameterDAO;
import com.angel.architecture.daos.ParameterAreaDAO;
import com.angel.architecture.persistence.beans.ConfigurationParameter;
import com.angel.architecture.persistence.beans.ParameterArea;
import com.angel.common.helpers.StringHelper;
import com.angel.data.generator.annotations.Inject;
import com.angel.io.annotations.ColumnRow;
import com.angel.io.annotations.RowChecker;
import com.angel.io.annotations.RowProcessor;
import com.angel.io.annotations.RowProcessorCommand;
import com.angel.io.exceptions.InvalidRowDataException;

/**
 * @author William
 *
 */
@RowProcessorCommand
	(
		columnsRow = {
			@ColumnRow( columnName = ConfigurationParameterAnnotationRowProcessorCommand.NAME_COLUMN, aliases = {"Nombre del Parametro"} ),
			@ColumnRow( columnName = ConfigurationParameterAnnotationRowProcessorCommand.DESCRIPTION_COLUMN, aliases = {"Descripcion del Parametro"} ),
			@ColumnRow( columnName = ConfigurationParameterAnnotationRowProcessorCommand.PARAMETER_AREA_NAME_COLUMN, aliases = {"Nombre del area del Parametro"} ),
			@ColumnRow( columnName = ConfigurationParameterAnnotationRowProcessorCommand.MINIMUM_VALUE_COLUMN, aliases = {"Valor Minimo"}),
			@ColumnRow( columnName = ConfigurationParameterAnnotationRowProcessorCommand.MAXIMUM_VALUE_COLUMN, aliases = {"Valor Maximo"}),
			@ColumnRow( columnName = ConfigurationParameterAnnotationRowProcessorCommand.STRING_VALUE_COLUMN, aliases = {"Valor de Cadena"} ),
			@ColumnRow( columnName = ConfigurationParameterAnnotationRowProcessorCommand.LONG_VALUE_COLUMN, aliases = {"Valor tipo Long"}, type = Long.class),
			@ColumnRow( columnName = ConfigurationParameterAnnotationRowProcessorCommand.DOUBLE_VALUE_COLUMN, aliases = {"Valor Tipo Double"}, type = Double.class),
			@ColumnRow( columnName = ConfigurationParameterAnnotationRowProcessorCommand.FLOAT_VALUE_COLUMN, aliases = {"Valor Tipo Float"}, type = Float.class),
			@ColumnRow( columnName = ConfigurationParameterAnnotationRowProcessorCommand.CHARACTER_VALUE_COLUMN, aliases = {"Valor Tipo Character"}, type = Character.class),
			@ColumnRow( columnName = ConfigurationParameterAnnotationRowProcessorCommand.BOOLEAN_VALUE_COLUMN, aliases = {"Valor Tipo Boolean"}, type = Boolean.class),
			@ColumnRow( columnName = ConfigurationParameterAnnotationRowProcessorCommand.DATE_VALUE_COLUMN, aliases = {"Valor Tipo Date"})
		}
	)
public class ConfigurationParameterAnnotationRowProcessorCommand {

	public static final String NAME_COLUMN = "name";
	public static final String DESCRIPTION_COLUMN = "description";
	public static final String PARAMETER_AREA_NAME_COLUMN = "parameterAreaName";
	public static final String MINIMUM_VALUE_COLUMN = "minimumValue";
	public static final String MAXIMUM_VALUE_COLUMN = "maximumValue";
	public static final String STRING_VALUE_COLUMN = "stringValue";
	public static final String LONG_VALUE_COLUMN = "longValue";
	public static final String DOUBLE_VALUE_COLUMN = "doubleValue";
	public static final String FLOAT_VALUE_COLUMN = "floatValue";
	public static final String CHARACTER_VALUE_COLUMN = "characterValue";
	public static final String BOOLEAN_VALUE_COLUMN = "booleanValue";
	public static final String DATE_VALUE_COLUMN = "dateValue";

	@Inject
	private ConfigurationParameterDAO configurationParameterDAO;
	@Inject
	private ParameterAreaDAO parameterAreaDAO;

    @RowChecker(columnsParameters = {})
    public void checkRowData(String name,String description,String parameterAreaName, String minimumValue, String maximumValue,
    		String stringValue, Long  longValue, Double doubleValue, Float floatValue, Character characterValue,
    		Boolean booleanValue, String dateValue) throws InvalidRowDataException {
    	
		boolean areAllNotEmpty = StringHelper.areAllNotEmpty(name, description, parameterAreaName,
				longValue.toString(), doubleValue.toString(), 
				floatValue.toString(), booleanValue.toString());

		if(!areAllNotEmpty){
			throw new InvalidRowDataException("Some row data are NULL - " +
					"name: [" + name + "] - " +
					"description: [" + description + "] - " +
					"parameterAreaName: [" + parameterAreaName + "] - " +
					"longValue: [" + longValue + "] - " +
					"doubleValue: [" + doubleValue + "] - " +
					"floatValue: [" + floatValue + "] - " +
					"booleanValue: [" + booleanValue + "].");
		}
		ConfigurationParameter configurationParameter = configurationParameterDAO.findUniqueOrNullByName(name);
		if(configurationParameter != null){
			throw new InvalidRowDataException("Configuration parameter exist with name [" + name + "] and id [" + configurationParameter.getIdAsString() + "].");
		}
		ParameterArea parameterArea = this.getParameterAreaDAO().findUniqueOrNullByName(parameterAreaName);
		if(parameterArea == null){
			throw new InvalidRowDataException("Parameter area doesn't exist with name [" + parameterAreaName + "].");
		}
    }

	@RowProcessor(columnsParameters = {}, object = ConfigurationParameter.class, inject = true)
	public ConfigurationParameter processRow(ConfigurationParameter configurationParameter, String name,
			String description,String parameterAreaName, String minimumValue, String maximumValue,
    		String stringValue, Long  longValue, Double doubleValue, Float floatValue, Character characterValue,
    		Boolean booleanValue, String dateValue) {
		ParameterArea parameterArea = this.getParameterAreaDAO().findUniqueOrNullByName(parameterAreaName);
		configurationParameter.setParameterArea(parameterArea);
        return configurationParameter;
    }

	/**
	 * @return the configurationParameterDAO
	 */
	public ConfigurationParameterDAO getConfigurationParameterDAO() {
		return configurationParameterDAO;
	}

	/**
	 * @param configurationParameterDAO the configurationParameterDAO to set
	 */
	public void setConfigurationParameterDAO(
			ConfigurationParameterDAO configurationParameterDAO) {
		this.configurationParameterDAO = configurationParameterDAO;
	}

	/**
	 * @return the parameterAreaDAO
	 */
	public ParameterAreaDAO getParameterAreaDAO() {
		return parameterAreaDAO;
	}

	/**
	 * @param parameterAreaDAO the parameterAreaDAO to set
	 */
	public void setParameterAreaDAO(ParameterAreaDAO parameterAreaDAO) {
		this.parameterAreaDAO = parameterAreaDAO;
	}
}
